var $;
layui.config({
    base: "../../js/"
}).use(['form', 'layer', 'jquery', 'laytpl'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        laytpl = layui.laytpl,
        $ = layui.jquery;

    var data = window.parent.layer.getArgs("data");
    //获取用户详细信息
    $.get("/shiro/user/info/" + parseInt(data), function (r) {
        var getTpl = page.innerHTML
            , view1 = document.getElementById('view1');
        laytpl(getTpl).render(r, function (html) {
            view1.innerHTML = html;
            $.get("/shiro/role/select", function (a) {
                var getTpl = role.innerHTML
                    , view = document.getElementById('view');
                laytpl(getTpl).render(a, function (html) {
                    view.innerHTML = html;
                    var values = r.userList.roleIdList;
                    for (var i = 0; i < r.userList.roleIdList.length; i++) {
                        var tem = r.userList.roleIdList[i];
                        $("input[type='checkbox'][name='poetrytype'][value='" + tem + "']").attr("checked", true);

                    }
                });
                form.render(null, 'formresh');
            });
        });

    });


    var addUserArray = [],updateUser,roleIdList=[];
    form.on("submit(updateUser)",function(data){
        //是否添加过信息
        if(window.sessionStorage.getItem("updateUser")){
            addUserArray = JSON.parse(window.sessionStorage.getItem("updateUser"));
        }
        $("input:checkbox[name='poetrytype']:checked").each(function(){
            roleIdList.push($(this).val());
        });
        roleIdList="["+roleIdList+"]";
        updateUser = '{"nickname":"'+ $(".nickName").val() +'",';  //登录名
        updateUser += '"id":"'+ $(".id").val() +'",';	 //id
        updateUser += '"salt":"'+ $(".salt").val() +'",';	 //salt
        updateUser += '"username":"'+ $(".userName").val() +'",';	 //用户名
        updateUser += '"email":"'+ $(".userEmail").val() +'",';	 //邮箱
        updateUser += '"password":"'+ $(".password").val() +'",'; //密码
        updateUser += '"status":"'+ data.field.userStatus +'",'; //状态
        updateUser += '"roleIdList": '+  roleIdList +'}';  //角色
        console.log(JSON.stringify(JSON.parse(updateUser)));
        addUserArray.unshift(JSON.parse(updateUser));
        window.sessionStorage.setItem("updateUser",JSON.stringify(addUserArray));
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            type: "POST",
            url:   "/shiro/user/edituser",
            contentType: "application/json",
            data: JSON.stringify(JSON.parse(updateUser)),
            success: function(r){
                if(r.code === 0){
                    top.layer.msg("用户修改成功！");

                }else{
                    alert(r.msg);
                }
            }
        });
        top.layer.close(index);
        layer.closeAll("iframe");
        //刷新父页面
       parent.location.reload();
        return false;
    })



});